package wechat.handler.response.business.event.menu;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import wechat.handler.bean.response.event.menu.MenuScancodeWaitmsgBean;
import wechat.handler.resources.AppFilter;
import wechat.handler.resources.InstanceResource;
import wechat.handler.util.date.DateUtil;
import wechat.handler.work.menu.MenuScancodeWaitmsgHandler;
import wechat.handler.work.template.SendTempleHandler;

public class MenuScancodeWaitmsgBusiness {
	private Logger log = LoggerFactory.getLogger(MenuScancodeWaitmsgBusiness.class);
	
	private MenuScancodeWaitmsgBean menuScancodeWaitmsgBean;
	private InstanceResource instanceResource;
	
	private static final String PayCodeStr = "payCode";
	
	public MenuScancodeWaitmsgBusiness(MenuScancodeWaitmsgBean menuScancodeWaitmsgBean){
		this.menuScancodeWaitmsgBean = menuScancodeWaitmsgBean;
	}
	
	public MenuScancodeWaitmsgBusiness(String appid,MenuScancodeWaitmsgBean menuScancodeWaitmsgBean){
		if (appid != null) {
			instanceResource = AppFilter.chooseApp(appid);
		}
		this.menuScancodeWaitmsgBean = menuScancodeWaitmsgBean;
	}
	
	public MenuScancodeWaitmsgBusiness(String str) throws Exception{
		this.menuScancodeWaitmsgBean = new MenuScancodeWaitmsgBean(str);
		this.menuScancodeWaitmsgBean.transform();
	}
	
	public MenuScancodeWaitmsgBusiness(String appid,String str) throws Exception{
		if (appid != null) {
			instanceResource = AppFilter.chooseApp(appid);
		}
		this.menuScancodeWaitmsgBean = new MenuScancodeWaitmsgBean(str);
		this.menuScancodeWaitmsgBean.transform();
	}
	
	public void bindCode(){
		String scanResult = menuScancodeWaitmsgBean.getScanResult();
		String openid = menuScancodeWaitmsgBean.getFromUserName();
		
		if (!scanResult.contains("exam.com")) {
			log.error("EventScancodeWaitmsgBusiness---bindCode  Domain name is wrong ： " + scanResult);
			
			try {
				
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("template_id","Bind");
				jsonObject.put("touser",openid);
				jsonObject.put("url","");
				jsonObject.put("first","收款码无法识别，绑定失败");
				jsonObject.put("keyword1","");
				jsonObject.put("keyword2",DateUtil.getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));
				jsonObject.put("remark","如果有疑问，请咨询4000-365-724");
				
				SendTempleHandler sendTempleHandler = new SendTempleHandler();
				sendTempleHandler.sendTemple(instanceResource.getAppid(),jsonObject);
			} catch (Exception e) {
				log.error("EventScancodeWaitmsgBusiness---bindCode  fail ： ",e);
				e.printStackTrace();
			}
			
			return;
		}
		
		MenuScancodeWaitmsgHandler menuScancodeWaitmsgHandler = new MenuScancodeWaitmsgHandler();
		String type = menuScancodeWaitmsgBean.getEventKey().split("_")[0];
		String payCode = scanResult.substring(scanResult.indexOf(PayCodeStr) + PayCodeStr.length() + 1);
		
		menuScancodeWaitmsgHandler.buildBindCode(openid, type, payCode);
	}

	public InstanceResource getInstanceResource() {
		return instanceResource;
	}
}
